$(document).ready(function () {
    loadAll();
    $(".Info-row").each(function(){
        $("#auth-btn",this).click(function(){
            var search_phone = $(this).parents("tr").find(".StaffPhone").text();
            var telephone = localStorage.getItem(search_phone);
            var info = JSON.parse(telephone);
            // console.log(info)
            // console.log(info)----打印数据，对象
            getDom();
            valuetion(info);
            $("#ConfirmSubmit").click(function () {
                localStorage.removeItem(info.phone); // 移除原有数据
                changeNews(info)
                var strOne = JSON.stringify(info);
                localStorage.setItem(info.phone,strOne);
                loadAll();
                window.location.href = "../view/staffInfo.html"
            })
        })
        //改动值,重定义
        function changeNews(info) {
            info.name = $("#data_name").val();
            info.sex = $("#data_sex").val();
            info.phone = $("#data_phone").val();
            info.id = $("#data_id").val();
            info.part = $("#data_part").val();
            info.job = $("#data_job").val();
            info.door = $("#data_door").val();
            info.type = $("#data_type").val();
        }
        //获取页面节点
        function getDom() {
            var data_name = document.getElementById("data_name");//无法用$
            var data_sex = document.getElementById("data_sex");
            var data_phone = document.getElementById("data_phone");
            var data_id = document.getElementById("data_id");
            var data_part = document.getElementById("data_part");
            var data_job = document.getElementById("data_job");
            var data_door = document.getElementById("data_door");
            var data_type = document.getElementById("data_type");
        }
        //模态框赋值
        function valuetion(info) {
            data_name.value = info.name;
            data_sex.value = info.sex;
            data_phone.value = info.phone;
            data_id.value = info.id;
            data_part.value = info.part;
            data_job.value = info.job;
            data_door.value = info.door;
            data_type.value = info.type;
        }
    })

})

var infoList = {
            content:[
                {
                    name:"王氏", gender:"男", telephone:"13315665865", ID:"130481188765443213",
                    part:"项目部", job:"经理", door:"006666", type:"未认证"
                },
                {
                    name:"海天", gender:"男", telephone:"15715665865", ID:"130491188765443213",
                    part:"ce人事部", job:"经理", door:"010016", type:"未认证"
                },
                {
                    name:"昌盛", gender:"男", telephone:"13369851256", ID:"130491188765443213",
                    part:"ce人事部", job:"经理", door:"013316", type:"未认证"
                }
            ]
};
// $('#staffList').tmpl(infoList).appendTo('.staffInfo-list')
//保存数据
function init(){
    for(i=0;i<infoList.content.length;i++){
        var nameList = infoList.content[i].name;
        var genderList = infoList.content[i].gender;
        var phoneList = infoList.content[i].telephone;
        var idList = infoList.content[i].ID;
        var partList = infoList.content[i].part;
        var jobList = infoList.content[i].job;
        var doorList = infoList.content[i].door;
        var typeList = infoList.content[i].type;
        var contact = {
            "name":nameList,"sex":genderList,"phone":phoneList,
            "id":idList,"part":partList,"job":jobList,"door":doorList,"type":typeList
        }
        console.log(contact)
        var str = JSON.stringify(contact);
        localStorage.setItem(contact.phone,str);
    }
    loadAll();
}
//将所有存储在localStorage中的对象提取出来，并展现到界面上
function loadAll(){
    var list = document.getElementById("staffInfo-list");
    if(localStorage.length>0){
        var result = "<table class='table box-table'>";
        result += "<tr class='box-table-tr'>" +
            "<td>姓名</td>" +
            "<td>性别</td>" +
            "<td>联系电话</td>" +
            "<td>身份证号码</td>" +
            "<td>专业</td>" +
            "<td>职位</td>" +
            "<td>门卡号</td>" +
            "<td>状态</td>" +
            "<td></td>" +
            "</tr>";
        eachInfo(result,list)
    }else{
        list.innerHTML = "目前数据为空，请加入数据" +
            "<button id='addStaffInfo'>添加</button>";
    }
    $("#addStaffInfo").click(function () {
        init();
        window.location.href = "../view/staffInfo.html"
    })
}
function eachInfo(result,list) {
    for(var i=0;i<localStorage.length;i++){
        var phone = localStorage.key(i);
        var str = localStorage.getItem(phone);
        var contact = JSON.parse(str);
        result += "<tr class='Info-row'>" +
            "<td>"+contact.name+"</td>" +
            "<td>"+contact.sex+"</td>" +
            "<td class='StaffPhone'>"+contact.phone+"</td>" +
            "<td>"+contact.id+"</td>" +
            "<td>"+contact.part+"</td>" +
            "<td>"+contact.job+"</td>" +
            "<td>"+contact.door+"</td>" +
            "<td>"+contact.type+"</td>" +
            "<td class='form-list form-list-write'><button id='auth-btn' data-toggle='modal' data-target='#changeStaffInfo'>修改</button></td>" +
            "</tr>";
    }
    result += "</table>";
    list.innerHTML = result;
}
